package rob;

public class Solution {
      public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
        TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

    public int rob(TreeNode root) {
          int[] ans = dfs(root);
          return Math.max(ans[0],ans[1]);
    }
    private int[] dfs(TreeNode root) {
          if(root == null){
              return new int[]{0,0};
          }
          int[] l = dfs(root.left);
          int[] r = dfs(root.right);
          int choose = root.val+l[1]+r[1];
          int nochoose = Math.max(l[0],l[1])+Math.max(r[0],r[1]);
          return new int[]{choose,nochoose};
    }
}
